package cn.kzgg.org.controller;


import cn.kzgg.basic.util.AjaxResult;
import cn.kzgg.basic.util.PageList;
import cn.kzgg.org.domain.Employee;
import cn.kzgg.org.query.EmployeeQuery;
import cn.kzgg.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 按id查
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee queryById(@PathVariable("id") Long id){
       return employeeService.findById(id);
    }

    /**
     * 查所有
     * @param
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查所有" )
    public List<Employee> queryAll(){
        return employeeService.findAll();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.removeById(id);
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("网络繁忙");
        }
    }

    /**
     * 新增修改
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增或修改" )
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);

            }else{
                employeeService.update(employee);
            }
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setMessage("网络繁忙").setSuccess(false);
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("网络繁忙");
        }
    }

    /**
     * 查询店铺的店员
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> queryShopEmployee(@PathVariable("shopId") Long shopId){
        return employeeService.queryShopEmployee(shopId);
    }

}
